package dice.sinanya.dice.manager;

import dice.sinanya.db.des.InsertDes;
import dice.sinanya.db.des.SelectDes;
import dice.sinanya.entity.EntityTypeMessages;
import org.nlpcn.commons.lang.util.StringUtil;

import static dice.sinanya.system.MessagesTag.TAG_DES_GET;
import static dice.sinanya.system.MessagesTag.TAG_DES_SET;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;

public class Des {
    EntityTypeMessages entityTypeMessages;

    public Des(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void set() {
        String tag = TAG_DES_SET;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        if (msg.contains(" ") && msg.split(" ").length >= 2) {
            String key = msg.split(" ")[0];
            String value = msg.substring(msg.indexOf(" ") + 1);
            new InsertDes().insertDes(entityTypeMessages.getFromQq(), key, value);
            sender(entityTypeMessages, String.format("场景%s保存成功", key));
        } else {
            sender(entityTypeMessages, "格式错误");
        }
    }

    public void get() {
        String tag = TAG_DES_GET;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        sender(entityTypeMessages, new SelectDes().selectDes(entityTypeMessages.getFromQq(), msg));
    }

    public void list() {
        sender(entityTypeMessages, "您当前已录入的场景列表为:\n" + StringUtil.joiner(new SelectDes().selectDesList(entityTypeMessages.getFromQq()), "\n"));
    }
}
